
/**
 * @description 冒泡排序的两种写法
 * @param {Array} arr 
 * @returns arr
 */
function bubbleSort(arr){
    const len = arr.length;

    for (let i = 0; i < len; i++) {
      const element = arr[i];
      for (let j = 0; j < len-i-1; j++) {
        const element = arr[j];
        
        if(arr[j] > arr[j+1]){
          const temp = arr[j+1]   //这里临时变量赋值为小的
            arr[j+1] = arr[j]
            arr[j] = temp
        }
      }
    }
    return arr
}


function bubbleSort2(arr){
  const len = arr.length;

  for (let i = 0; i < len; i++) {
    const element = arr[i];
    for (let j = 0; j < len-i-1; j++) {
      const element = arr[j];
      if(arr[j] > arr[j+1]){
        [arr[j],arr[j+1]] = [arr[j+1],arr[j]]
      }
    }
  }
  return arr
}



//test
const data = [3,2,4,5,6,1,10,7]
const res = bubbleSort2(data)
console.log("🚀 ~ file: demo2.js:10 ~ res:", res)

/**
 * 🚀 ~ file: demo2.js:10 ~ res: [1, 2, 3,  4,5, 6, 7, 10]
 */
